﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Heyu.Utility
{
    public class ProgressBarUtility
    {
        #region 进度条html代码参考

        //            <style type="text/css">
        //        .progressbar { width: 500px; height: 30px; margin: 0 auto; background: #fff; border: 1px solid #000; }
        //        .bar { width: 0%; height: 30px; line-height: 30px; background-color: red; text-align: center; color: #000; }
        //    </style>

        //    <script language="javascript">
        //        function BeginPorgressBar() {
        //            $(".progressbar").show();
        //            setProgress('0%', '正在获取数据,请稍等...');
        //        }
        //        function SetPorgressBar(percent, value) {
        //            $(".bar").css("width", percent);
        //            $(".bar").html(value);
        //        }

        //        //完成后隐藏进度条
        //        function SetCompleted() {
        //            $(".progressbar").hide();
        //        }
        //    </script>




        //<body>
        //    <div class="progressbar">
        //        <div class="bar"></div>
        //    </div>
        //</body>

        #endregion

        /// <summary>
        /// 准备进度条,显示到界面上(进度条存在htm页面中)
        /// </summary>
        /// <param name="htmlpage"></param>
        public static void BeginProgress(string htmlpage = "ProgressBar.htm")
        {
            //根据ProgressBar.htm显示进度条界面
            string templateFileName = Path.Combine(HttpContext.Current.Server.MapPath("."), htmlpage);
            StreamReader reader = new StreamReader(@templateFileName, System.Text.Encoding.GetEncoding("GB2312"));
            string html = reader.ReadToEnd();
            reader.Close();
            HttpContext.Current.Response.Write(html);
            HttpContext.Current.Response.Flush();
        }

        /// <summary>
        /// 设置进度条的位置
        /// </summary>
        /// <param name="percent">百分比</param>
        /// <param name="value">显示的文字</param>
        public static void SetProgress(string percent, string value)
        {
            string jsBlock = "<script>SetPorgressBar('" + percent.ToString() + "','" + value + "'); </script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
        }
        /// <summary>
        /// 进度条完成后,隐藏进度条.
        /// </summary>
        public static void EndProgress()
        {
            string jsBlock = "<script>SetCompleted();</script>";
            HttpContext.Current.Response.Write(jsBlock);
            HttpContext.Current.Response.Flush();
        }
    }
}
